package com.lczc.soa.controller;


import com.alibaba.fastjson.JSONObject;
import com.lczc.soa.domain.*;
import com.lczc.soa.domain.SearchApplyAgainResponse;
import com.lczc.soa.service.ApplyAgainService;
import com.lczc.soa.service.ApplyAgainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/ApplyAgain")
public class ApplyAgainController {

    @ModelAttribute
    public void checkIfAdmin(HttpServletRequest request,Model model){
        Object user = request.getSession().getAttribute("current_user");
        if(user instanceof  User){
            model.addAttribute("ifAdmin","0");
        }else{
            model.addAttribute("ifAdmin","1");

        }
    }

    @Autowired
    private ApplyAgainService applyAgainService;

    @RequestMapping("/toadd")
    public String addApplyAgain(Model model){
        return "apply_again_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public  String addApplyAgain(ApplyAgain applyAgain, HttpServletRequest request){
        Object currentUser = request.getSession().getAttribute("current_user");
        if (currentUser!=null){
            if (currentUser instanceof User){
                User user = (User)currentUser ;
                applyAgain.setUserId(user.getUserId());
                applyAgain.setUserName(user.getUserName());
                applyAgain.setCreateDate(new Date());
            }else if(currentUser instanceof Admin){
                System.out.println("admin update the state");
            }
        }
        applyAgain.setContent(request.getAttribute("content").toString());
        int insert= applyAgainService.saveApplyAgain(applyAgain);
        JSONObject jsonObject = new JSONObject();
        if (insert>0){
            jsonObject.put("code","0000");
            jsonObject.put("msg","保存成功！");
            return jsonObject.toJSONString();
        }
        jsonObject.put("code","9999");
        jsonObject.put("msg","保存失败！");
        return jsonObject.toJSONString();
    }

    @RequestMapping("/list")
    public String ApplyAgainList() {
        return "apply_again_list";
    }

    @RequestMapping("/list_user")
    public String ApplyAgainListUser() {
        return "apply_again_list_user";
    }

    @GetMapping("/api/search")
    @ResponseBody
    public SearchApplyAgainResponse searchApplyAgainResponse(@RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize) {

        SearchApplyAgainResponse allApplyAgain = applyAgainService.findAllApplyAgain(pageNum, pageSize);
        return allApplyAgain;
    }

    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchApplyAgainResponse searchApplyAgainResponseLike(@RequestParam("keywords") String keywords,
                                                         @RequestParam("pageNum") Integer pageNum,
                                                         @RequestParam("pageSize") Integer pageSize) {
        return applyAgainService.findApplyAgainLikeName(keywords, pageNum, pageSize);
    }
}
